package com.ruoyi.college.service.impl;

import com.ruoyi.college.domain.AdminClassTrainingLink;
import com.ruoyi.college.mapper.AdminClassTrainingLinkMapper;
import com.ruoyi.college.service.IAdminClassTrainingLinkService;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 班级培养方案中间表Service业务层处理
 * 
 * @author chenxl
 * @date 2025-05-19
 */
@Service
public class AdminClassTrainingLinkServiceImpl implements IAdminClassTrainingLinkService 
{
    @Autowired
    private AdminClassTrainingLinkMapper adminClassTrainingLinkMapper;
    private static final Logger log = LoggerFactory.getLogger(AdministrativeClassServiceImpl.class);
    /**
     * 查询班级培养方案中间表
     * 
     * @param id 班级培养方案中间表主键
     * @return 班级培养方案中间表
     */
    @Override
    public AdminClassTrainingLink selectAdminClassTrainingLinkById(String id)
    {
        return adminClassTrainingLinkMapper.selectAdminClassTrainingLinkById(id);
    }

    /**
     * 查询班级培养方案中间表列表
     * 
     * @param adminClassTrainingLink 班级培养方案中间表
     * @return 班级培养方案中间表
     */
    @Override
    public List<AdminClassTrainingLink> selectAdminClassTrainingLinkList(AdminClassTrainingLink adminClassTrainingLink)
    {
        return adminClassTrainingLinkMapper.selectAdminClassTrainingLinkList(adminClassTrainingLink);
    }

    @Override
    public int insertAdminClassTrainingLink(AdminClassTrainingLink adminClassTrainingLink) {
        return 0;
    }

    /**
     * 批量插入班级-培养方案中间表
     *
     * @param adminClassTrainingLinks 中间表数据列表
     * @return 受影响的行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int batchInsertAdminClassTrainingLink(List<AdminClassTrainingLink> adminClassTrainingLinks) {
        if (CollectionUtils.isEmpty(adminClassTrainingLinks)) {
            return 0;
        }

        try {
            // 1. 设置唯一ID（如果数据库不自增）
            adminClassTrainingLinks.forEach(link -> {
                if (link.getId() == null) {
                    link.setId(IdUtils.fastSimpleUUID());
                }
            });

            // 2. 调用 Mapper 批量插入
            int rows = adminClassTrainingLinkMapper.batchInsertAdminClassTrainingLink(adminClassTrainingLinks);

            // 3. 返回受影响的行数
            return rows;
        } catch (Exception e) {
            log.error(() -> "批量插入中间表失败", e);
            throw new RuntimeException("批量插入失败: " + e.getMessage());
        }
    }

    /**
     * 修改班级培养方案中间表
     * 
     * @param adminClassTrainingLink 班级培养方案中间表
     * @return 结果
     */
    @Override
    public int updateAdminClassTrainingLink(AdminClassTrainingLink adminClassTrainingLink)
    {
        return adminClassTrainingLinkMapper.updateAdminClassTrainingLink(adminClassTrainingLink);
    }

    /**
     * 批量删除班级培养方案中间表
     * 
     * @param ids 需要删除的班级培养方案中间表主键
     * @return 结果
     */
    @Override
    public int deleteAdminClassTrainingLinkByIds(String[] ids)
    {
        return adminClassTrainingLinkMapper.deleteAdminClassTrainingLinkByIds(ids);
    }

    /**
     * 删除班级培养方案中间表信息
     * 
     * @param id 班级培养方案中间表主键
     * @return 结果
     */
    @Override
    public int deleteAdminClassTrainingLinkById(String id)
    {
        return adminClassTrainingLinkMapper.deleteAdminClassTrainingLinkById(id);
    }
    @Override
    public List<String> listTrainingProgramIdsByClassId(String classId) {
        return adminClassTrainingLinkMapper.selectTrainingProgramIdsByClassId(classId);
    }

}
